package data_report

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PeriodsReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 期数报表
func NewPeriodsReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PeriodsReportLogic {
	return &PeriodsReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PeriodsReportLogic) PeriodsReport(req *types.PeriodsReportRequest) (resp *types.PeriodsReportResponse, err error) {
	resp = &types.PeriodsReportResponse{
		Data:     make([]types.PeriodsReportData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	periodsAnalysisReq := &rpc.SearchArPeriodsAnalysisReq{
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		Issue:     req.Issue,
		Page:      req.Page,
		PageSize:  req.PageSize,
	}
	periodsAnalysisResp, err := l.svcCtx.Analysis.SearchArPeriodsAnalysis(l.ctx, periodsAnalysisReq)
	if err != nil {
		return nil, err
	}

	copier.Copy(&resp.Data, periodsAnalysisResp.List)
	resp.Total = periodsAnalysisResp.Total
	return
}
